package com.chinairi.mall.controller;


import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.service.ISysUserService;
import com.chinairi.mall.service.modelobject.CiccLabelMO;
import com.chinairi.mall.service.modelobject.NewUserMO;
import com.chinairi.mall.service.modelobject.UserInfoToRedisMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 员工基本信息(如果客户已有人力资源的用户信息表，则使用客户提供的表，不用该表) 前端控制器
 * </p>
 *
 * @author 董志涵
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/sys-user")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 用户注册和企业资质
     *
     * @param registerJson 请求json串
     * @param request      请求
     * @throws IOException 异常
     */
    @PostMapping("/register")
    public void register(@RequestBody String registerJson, HttpServletRequest request) throws Exception {
        iSysUserService.register(registerJson, request);
        ResponseUtil.showMessage("success", "用户注册成功", null);
    }

    /**
     * 更新用户信息
     *
     * @param userInfoToRedisMo 用户信息
     * @throws Exception 异常
     */
    @RequestMapping("/updateSysUserInfo")
    public void updateSysUserInfo(@RequestBody UserInfoToRedisMO userInfoToRedisMo) throws Exception {
        iSysUserService.updateSysUserInfo(userInfoToRedisMo);
        ResponseUtil.showMessage("success", "用户信息", "修改成功");
    }

    /**
     * 获取图片验证码
     *
     * @param response 返回
     * @throws Exception 异常
     */
    @GetMapping("/verificationCode")
    public void verificationCode(HttpServletResponse response) throws Exception {
        iSysUserService.verificationCode(response);
    }

    /**
     * 手机号码查重+发送验证码
     *
     * @param phoneNumber 手机号码
     * @param request     请求
     * @throws Exception 异常
     */
    @GetMapping("/checkPhoneNumber")
    public void checkPhoneNumber(String phoneNumber, HttpServletRequest request) throws Exception {
        ResponseUtil.showMessage("success", "验证码已发送", iSysUserService.checkPhoneNumber(phoneNumber, request));
    }

    /**
     * 用户更换手机号码绑定
     *
     * @param phone   手机号码
     * @param request 请求
     * @throws IOException //@MyLog(value = "给用户绑定otp验证码")  //这里添加了AOP的自定义注解
     */
    @PutMapping("/updatePhone")
    @ResponseBody
    public void updatePhone(String phone, HttpServletRequest request) throws IOException {
        iSysUserService.updatePhone(phone, request);
        ResponseUtil.showMessage("success", "手机号码绑定成功", null);
    }

    /**
     * 验证某个表中是否存在某字段
     *
     * @param table   表名
     * @param clo     字段名
     * @param value   字段值
     * @param title   标签
     * @param primary 主键字段名
     * @param key     主键值
     * @throws IOException 异常
     */
    @GetMapping("/checkExist")
    public void checkExist(String table, String clo, String value, String title, String primary, String key) throws IOException {
        int check = iSysUserService.checkExist(table, clo, value, primary, key);
        if (check > 0) {
            ResponseUtil.showMessage("fail", "请求成功", title + "已存在");
        } else {
            ResponseUtil.showMessage("SUCCESS", "请求成功", title + "可以使用");
        }

    }

    /**
     * 获取用户基本信息
     *
     * @param request
     * @throws IOException
     */
    @GetMapping("/getSysUserInfor")
    @ResponseBody
    public void getUserInfo(HttpServletRequest request) throws IOException {
        ResponseUtil.showMessage("success", "获取用户信息成功", iSysUserService.getUserInfo(request));
    }

    /**
     * 子用户新增
     *
     * @param newUserJson
     * @param request
     * @throws Exception
     */
    @PostMapping("/addNewUser")
    public void addNewUser(@RequestBody String newUserJson, HttpServletRequest request) throws Exception {
        iSysUserService.addNewUser(newUserJson, request);
        ResponseUtil.showMessage("success", "子用户注册成功", "");
    }

    /**
     * 子用户列表获取
     *
     * @throws Exception
     */
    @GetMapping("/getNewUserInfo")
    public void getNewUserInfo() throws Exception {
        List<NewUserMO> list = iSysUserService.getNewUserInfo();
        ResponseUtil.showMessage("success", "子用户列表获取成功", list);
    }

    /**
     * 修改子用户信息
     * @param newUserJson
     * @param request
     * @throws Exception
     */
    @PostMapping("/updateNewUser")
    public void updateNewUser(@RequestBody String newUserJson, HttpServletRequest request) throws Exception {
        iSysUserService.updateNewUser(newUserJson, request);
        ResponseUtil.showMessage("success", "子用户信息修改成功", "");
    }

    /**
     * 子用户删除
     *
     * @param userId
     * @throws Exception
     */
    @GetMapping("/deleteNewUser")
    public void deleteNewUser(String userId) throws Exception {
        iSysUserService.deleteNewUser(userId);
        ResponseUtil.showMessage("success", "子用户删除成功", "");
    }

    /**
     * 获取用户性别的编码表内容
     *
     * @throws Exception
     */
    @GetMapping("/getUserSexLabel")
    public void getUserSexLabel() throws Exception {
        List<CiccLabelMO> ciccLabelMoList = iSysUserService.getUserSexLabel();
        ResponseUtil.showMessage("success", "获取用户性别编码表成功", ciccLabelMoList);
    }

    /**
     * 获取用户状态的编码表内容
     *
     * @throws Exception
     */
    @GetMapping("/getUserStatusLabel")
    public void getUserStatusLabel() throws Exception {
        List<CiccLabelMO> ciccLabelMoList = iSysUserService.getUserStatusLabel();
        ResponseUtil.showMessage("success", "获取用户状态编码表成功", ciccLabelMoList);
    }

    /**
     * 获取用户状态的编码表内容
     *
     * @throws Exception
     */
    @GetMapping("/getAccountTypeLabel")
    public void getAccountTypeLabel() throws Exception {
        List<CiccLabelMO> ciccLabelMoList = iSysUserService.getAccountTypeLabel();
        ResponseUtil.showMessage("success", "获取用户状态编码表成功", ciccLabelMoList);
    }

    /**
     * 获取用户状态的编码表内容
     *
     * @throws Exception
     */
    @GetMapping("/getSysStatusLabel")
    public void getSysStatusLabel() throws Exception {
        List<CiccLabelMO> ciccLabelMoList = iSysUserService.getSysStatusLabel();
        ResponseUtil.showMessage("success", "获取用户状态编码表成功", ciccLabelMoList);
    }

}